//
//  WHPreviewImageVC.m
//  WHEasyBizLink
//
//  Created by shengmeng chen on 2024/11/27.
//

#import "WHPreviewImageVC.h"
#import "WHPreviewImageView.h"
#import "WHReSanViewController.h"
#import "WHPDFManager.h"
#import "CommonTools.h"
#import "WHFileManager.h"
#import "WHDialog.h"
#import "WHOpenPDFVC.h"

@interface WHPreviewImageVC()

@property (nonatomic,strong)WHPreviewImageView *previewImageView;

@property (nonatomic,strong)UIBarButtonItem *rightItem;

@end

@implementation WHPreviewImageVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"图片预览";
    self.view.backgroundColor = [UIColor systemBackgroundColor];
    [self.view addSubview:self.previewImageView];
    self.navigationItem.rightBarButtonItem =self.rightItem;
    __weak __typeof(self) weakSelf = self;
    self.previewImageView.clickBtnBlock = ^(WHPreviewBtnClickType btnType, int index) {
        if (btnType ==WHPreviewBtnClickDelate){
            [weakSelf showAlertControllerWithIndex:index];
        }else if (btnType ==WHPreviewBtnClickChange)
        {
            [[WHDialog sharedManager] showHudWithText:@"正在生成文件..."];
            dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC);
            dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
                //转换PDF
                NSString *name = [NSString stringWithFormat:@"%@.pdf",[CommonTools ret32BitString]];
                NSString*pdfPath = pdfPathStore(name);
                DebugLog(@"path%@",pdfPath);
                BOOL isCreate = [WHFileManager createFileAtPath:pdfPath];
                if (pdfPath.length >0 && self.imgArray.count>0 && isCreate) {
                    BOOL isSuccse =  [WHPDFManager convertImageToPDFWithPDFKit:weakSelf.imgArray outputFilePath:pdfPath];
                    if (isSuccse) {
                        [[WHDialog sharedManager] dismissWithView];
                        WHOpenPDFVC * vc = [[WHOpenPDFVC alloc] initWithFilePath:pdfPath];
                        [weakSelf.navigationController pushViewController:vc animated:YES];
                    }else{
                        [[WHDialog sharedManager] showHudErrorWithText:@"文件生成失败,请重试！"];
                    }
                }else{
                    [[WHDialog sharedManager] showHudErrorWithText:@"文件生成失败,请重试！"];
                }
            });
            
        };
    };
}

-(void)showAlertControllerWithIndex:(int)index
{
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"删除此页" message:@"删除后无法恢复，你也可以选择重拍这张进行替换" preferredStyle:UIAlertControllerStyleAlert];
   
    UIAlertAction *delateAction = [UIAlertAction actionWithTitle:@"删除" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        //移除
        [self.imgArray removeObjectAtIndex:index];
        if (self.delateRefleshBlock) {
            self.delateRefleshBlock();
        }
        //先移除，再实例化
        [self.previewImageView removeScrollViewSubView];
        self.previewImageView.imgArray = self.imgArray;
       
        if ((index-1)==self.imgArray.count-1) {
            if (index-1 >= 0) {
                self.previewImageView.indexpath = index-1;
            }
        }else{
            
            self.previewImageView.indexpath = index;
        }
    }];
    UIAlertAction *rephotoAction = [UIAlertAction actionWithTitle:@"重拍这张" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        WHReSanViewController*vc =[[WHReSanViewController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
        vc.reScanFinishBlock = ^(UIImage * _Nonnull image) {
            //替换
            [self.imgArray replaceObjectAtIndex:index withObject:image];
            if (self.delateRefleshBlock) {
                self.delateRefleshBlock();
            }
            //先移除，再实例化
            [self.previewImageView removeScrollViewSubView];
            self.previewImageView.imgArray = self.imgArray;
            self.previewImageView.indexpath = index;
        };
    }];
  
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
    }];
    // KVC 改变颜色
    [delateAction setValue:[UIColor redColor] forKey:@"_titleTextColor"];
    [alert addAction:delateAction];
    [alert addAction:rephotoAction];
    [alert addAction:cancelAction];
    alert.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentViewController:alert animated:YES completion:nil];
}

-(void)rightBarButtonItemClicked
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)setImgArray:(NSMutableArray<UIImage *> *)imgArray
{
    _imgArray = imgArray;
    self.previewImageView.imgArray = imgArray;
}

-(WHPreviewImageView *)previewImageView
{
    if(!_previewImageView)
    {
        _previewImageView = [[WHPreviewImageView alloc] initWithFrame:CGRectMake(0,navHeight1, ScreenW, ScreenH-navHeight1)];
    }
    return _previewImageView;
}
-(UIBarButtonItem *)rightItem
{
    if (!_rightItem)
    {
        
        UIButton* rightBtn= [[UIButton alloc] initWithFrame:CGRectMake(0,0,25,25)];
      [rightBtn addTarget:self action:@selector(rightBarButtonItemClicked)forControlEvents:UIControlEventTouchUpInside];
        [rightBtn setBackgroundImage:[UIImage imageNamed:@"photograph"] forState:UIControlStateNormal];
        _rightItem =[[UIBarButtonItem alloc]initWithCustomView:rightBtn];
    }
    return _rightItem;
}


@end
